#include <stdio.h>  
#include <stdlib.h>  
  
// 传感器类型枚举  
typedef enum {  
    SENSOR_TEMPERATURE,  
    SENSOR_HUMIDITY,  
    SENSOR_PRESSURE,  
    // 可以添加更多传感器类型  
} SensorType;  
  
// 传感器数据结构体  
typedef struct {  
    SensorType type;  
    float value;  
} SensorData;  
  
// 报警处理函数类型，现在它接收SensorData作为参数  
typedef void (*AlarmHandler)(SensorData data);  
  
// 报警节点结构体  
typedef struct AlarmNode {  
    SensorType type;       // 报警针对的传感器类型  
    double threshold;      // 报警阈值（这里改为double以支持更广泛的数据类型）  
    AlarmHandler handler;  // 处理函数  
    struct AlarmNode *next; // 指向下一个节点的指针  
} AlarmNode;  
  
// 全局链表的头指针  
static AlarmNode *head = NULL;  
  
// 注册报警处理函数  
void register_alarm(SensorType type, double threshold, AlarmHandler handler) {  
    AlarmNode *newNode = (AlarmNode *)malloc(sizeof(AlarmNode));  
    if (!newNode) {  
        perror("Failed to allocate memory for alarm node");  
        exit(EXIT_FAILURE);  
    }  
    newNode->type = type;  
    newNode->threshold = threshold;  
    newNode->handler = handler;  
    newNode->next = head;  
    head = newNode;  
}  
  
// 触发报警处理  
void trigger_alarms(SensorData data) {  
    AlarmNode *current = head;  
    while (current != NULL) {  
        if (current->type == data.type && data.value >= current->threshold) {  
             
            current->handler(data);  
        }  
        current = current->next;  
    }  
}  
  
// 报警处理函数的实现（示例仅针对温度）  
void handle_temperature_alarm_lv1(SensorData data) {  
    printf("handle_temperature_alarm_lv1: %f degrees\n", data.value); 
}
void handle_temperature_alarm_lv2(SensorData data) {  
    printf("handle_temperature_alarm_lv2: %f degrees\n", data.value); 
}

void handle_humidity_alarm_lv1(SensorData data) {  
    printf("handle_humidity_alarm_lv1: %f degrees\n", data.value); 
}
void handle_humidity_alarm_lv2(SensorData data) {  
    printf("handle_humidity_alarm_lv2: %f degrees\n", data.value); 
}   
  
int main() {  
    // 注册报警处理函数  
    register_alarm(SENSOR_TEMPERATURE, 70.0, handle_temperature_alarm_lv1);
	register_alarm(SENSOR_TEMPERATURE, 75.0, handle_temperature_alarm_lv2);
	register_alarm(SENSOR_HUMIDITY, 50.0, handle_humidity_alarm_lv1);
	register_alarm(SENSOR_HUMIDITY, 55.0, handle_humidity_alarm_lv2);	
  
    // 触发报警（示例数据）  
    SensorData tempData = {SENSOR_TEMPERATURE, 72};  
    trigger_alarms(tempData);  
	
	SensorData humiData = {SENSOR_HUMIDITY, 80};  
    trigger_alarms(humiData); 
  
    // 释放内存（在实际应用中需要遍历链表释放每个节点）  
    // 注意：这里的示例代码没有包含内存释放逻辑，以保持简洁。  
  
    return 0;  
}